<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%B8%80-memory-command/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">1666 字 ~8分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="redis4-0新特性-一-memory-command">Redis4.0新特性(一)-Memory Command</h1>

<blockquote>
<p>原文地址：<a href="https://www.jianshu.com/p/cd2951fa45f4">https://www.jianshu.com/p/cd2951fa45f4</a></p>
</blockquote>

<p>Redis4.0版本增加了很多诱人的新特性，在redis精细化运营管理中都非常有用(猜想和antirez加入redislabs有很大关系)；此系列几篇水文主要介绍以下几个新特性的使用和效果。</p>

<ul>
<li><p>Redis Memeory Command:详细分析内存使用情况，内存使用诊断，内存碎片回收；</p></li>

<li><p>PSYNC2:解决failover和从实例重启不能部分同步；<a href="https://github.com/antirez/redis/issues/4357">PSYNC3</a>已经路上了；</p></li>

<li><p>LazyFree: 再也不用怕big key的删除引起集群故障切换；</p></li>

<li><p>LFU: 支持近似的LFU内存淘汰算法；</p></li>

<li><p>Active Memory Defragmentation：内存碎片回收效果很好(实验阶段）；</p></li>

<li><p>Modules: Redis成为更多的可能（觉得像mongo/mysql引入engine的阶段）；</p></li>
</ul>

<h2 id="memory-command简介">Memory Command简介</h2>

<p>redis4.0引入新的命令memory, memory命令共有5个子命令；
让我们能更深入要了解redis内部的内存使用情况。
通过memory help命令，可以查看除memory doctor的其他4个子命令；
5个指令简介如下：</p>

<ul>
<li>MEMORY USAGE <key> [SAMPLES <count>] -&ldquo;Estimate memory usage of key&rdquo;</li>
<li>MEMORY STATS -&ldquo;Show memory usage details&rdquo;</li>
<li>MEMORY PURGE -&ldquo;Ask the allocator to release memory&rdquo;</li>
<li>MEMORY DOCTOR - &ldquo;A better observability on the Redis memory usage.&rdquo;</li>
<li>MEMORY MALLOC-STATS - &ldquo;Show allocator internal stats&rdquo;</li>
</ul>

<p>本文简述memory每个子命令的用途和部分实现。</p>

<h2 id="memory-usage">memory usage</h2>

<p>在redis4.0之前，只能通过<a href="https://link.jianshu.com/?t=https://redis.io/commands/debug-object">DEBUG OBJECT</a>命令估算key的内存使用(字段serializedlength)，但因为相差太大，没有太多参考价值。</p>

<blockquote>
<p>注：可以通过rdb工具分析rdb文件，获得某个key的实际使用内存</p>
</blockquote>

<p>如以下示例,k1的序列化值是7。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt; <span style="color:#204a87">set</span> k1 value1
OK
127.0.0.1:6379&gt; DEBUG OBJECT k1
xx refcount:1 encoding:embstr serializedlength:7 lru:7723189 lru_seconds_idle:160</code></pre></div>
<h4 id="memory-usage的基本使用">memory usage的基本使用</h4>

<p>usage子命令使用非常简单，直接按memory usage key名字；如果当前key存在，则返回key的value实际使用内存估算值；如果key不存在，则返回nil.
示例：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt; <span style="color:#204a87">set</span> k1 value1
OK
127.0.0.1:6379&gt; memory  usage k1    //这里k1 value占用57字节内存
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">57</span>
127.0.0.1:6379&gt; memory  usage aaa  // aaa键不存在，返回nil.
<span style="color:#ce5c00;font-weight:bold">(</span>nil<span style="color:#ce5c00;font-weight:bold">)</span></code></pre></div>
<h4 id="memory-usage细节分析">memory usage细节分析</h4>

<ul>
<li><p>memory usage不包含key串的内存占用</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt;set k1 a    // key长度为2字符
OK
127.0.0.1:6379&gt; memory usage k1  
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">52</span>
127.0.0.1:6379&gt; <span style="color:#204a87">set</span> k111111111111 a  //key长度为13字符
OK
127.0.0.1:6379&gt; memory usage k111111111111  //两个value相同,但key长度不同的key, usage分析的内存占用相同
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">52</span></code></pre></div></li>

<li><p>memory usage不包含Key Expire的内存占用</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt; memory usage k1  
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">52</span>
127.0.0.1:6379&gt; expire k1 <span style="color:#0000cf;font-weight:bold">10000</span>   //对k1设置ttl
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1</span>
127.0.0.1:6379&gt; memory usage k1  //usage不包含ttl的内存占用
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">52</span></code></pre></div></li>

<li><p>对于集合的数据类型(除string外), usage子命令采用类似LRU SAMPLES的抽样方式，
默认抽样5个元素求平均 X 元数个数 得出实际内存占用（下一节会详细说明）。所以计算是<strong>近似值</strong>，
当面可以指定抽样的SAMPLES个数。
示例说明: 生成一个100w个字段的hash键:hkey, 每字段的value长度是从1~1024字节的随机值。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">127.0.0.1:6379&gt; hlen hkey    // hkey有100w了字段，每个字段的value长度介入1~1024个字节
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1000000</span>
127.0.0.1:6379&gt; MEMORY usage hkey   //默认SAMPLES为5，分析hkey键内存占用521588753字节
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">521588753</span>
127.0.0.1:6379&gt; MEMORY usage hkey SAMPLES  <span style="color:#0000cf;font-weight:bold">1000</span> //指定SAMPLES为1000，分析hkey键内存占用617977753字节
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">617977753</span>
127.0.0.1:6379&gt; MEMORY usage hkey SAMPLES  <span style="color:#0000cf;font-weight:bold">10000</span> //指定SAMPLES为10000，分析hkey键内存占用624950853字节
<span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">624950853</span>
    </code></pre></div></li>
</ul>

<p>这是使用抽样求平均的算法，要想获取key较精确的内存值，就指定更大SAMPLES个数。
但并不越大越好，因为越大,memory usage占用cpu时间分片就大。</p>

<ul>
<li><p>memory usage时间复杂度，和指定的SAMPLES数有点
见以下示例，SAMPLES为1000耗时0.176ms, 为100000耗时14.65ms</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt; SLOWLOG get
1<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">3</span>
3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">14651</span>
4<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;MEMORY&#34;</span>
  2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;usage&#34;</span>
  3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;hkey&#34;</span>
  4<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;SAMPLES&#34;</span>
  5<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;100000&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1</span>
3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">176</span>
4<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;MEMORY&#34;</span>
  2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;usage&#34;</span>
  3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;hkey&#34;</span>
  4<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;SAMPLES&#34;</span>
  5<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1000&#34;</span></code></pre></div></li>
</ul>

<blockquote>
<p>注：全实例的Expire内存占用，详见下文memory stats子命令的overhead.hashtable.expires）</p>
</blockquote>

<h4 id="memory-usage源码实现">memory usage源码实现</h4>

<p>memory命令的入口函数为memoryCommand(object.c文件中）</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"> <span style="color:#8f5902;font-style:italic">/* The memory command will eventually be a complete interface for the
</span><span style="color:#8f5902;font-style:italic"> * memory introspection capabilities of Redis.
</span><span style="color:#8f5902;font-style:italic"> *
</span><span style="color:#8f5902;font-style:italic"> * Usage: MEMORY usage &lt;key&gt; */</span>
<span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">memoryCommand</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">client</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
</code></pre></div>
<p>对于memory usage的计算核心函数objectComputeSize(object.c文件中）
因为文章篇幅，这里只贴少部分代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">//函数注释已说明，value计算和取样计算的处理
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">/* Returns the size in bytes consumed by the key&#39;s value in RAM.
</span><span style="color:#8f5902;font-style:italic"> * Note that the returned value is just an approximation, especially in the
</span><span style="color:#8f5902;font-style:italic"> * case of aggregated data types where only &#34;sample_size&#34; elements
</span><span style="color:#8f5902;font-style:italic"> * are checked and averaged to estimate the total size. */</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">define OBJ_COMPUTE_SIZE_DEF_SAMPLES 5 </span><span style="color:#8f5902;font-style:italic">/* Default sample size. */</span><span style="color:#8f5902;font-style:italic">  </span><span style="color:#8f5902;font-style:italic">//对于集合类型结构，默认取样个数为5
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">size_t</span> <span style="color:#000">objectComputeSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">robj</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">size_t</span> <span style="color:#000">sample_size</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>

    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">OBJ_STRING</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">//String类型
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#a40000">省</span><span style="color:#a40000">略</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">type</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">OBJ_LIST</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">//List类型
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">encoding</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">OBJ_ENCODING_QUICKLIST</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">quicklist</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">ql</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">ptr</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">quicklistNode</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">node</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ql</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">head</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">asize</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">quicklist</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#8f5902;font-style:italic">//获取List的头sample_size个元数，计算长度之和
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#204a87;font-weight:bold">do</span> <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">elesize</span> <span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">quicklistNode</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000">ziplistBlobLen</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">node</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">zl</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000">samples</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">node</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">node</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">next</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">samples</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">sample_size</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#a40000">个</span><span style="color:#a40000">元</span><span style="color:#a40000">素</span>
            <span style="color:#000">asize</span> <span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">double</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">elesize</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">samples</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">listTypeLength</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">//求平均 X List元素个数，计算出内存之和
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000;font-weight:bold">}</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#a40000">省</span><span style="color:#a40000">略</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span>
</code></pre></div>
<h2 id="memory-stats">memory stats</h2>

<p>在redis 4.0之前，我们只能通过info memory查看redis实例的内存大体使用状况；而内存的使用细节，比如expire的消耗，client output buffer, query buffer等是很难直观显示的。 memory stats命令就是为展现redis内部内存使用细节。</p>

<h3 id="memory-stats的基本使用">memory stats的基本使用</h3>

<p>memory stats命令直接运行，返回当前实例内存使用细节；命令的系统开销小(可用于监控采集）
示例如下： 运行时返回33行数据，16个子项目； 下节详细分析，每个子项目的具体含义。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">127.0.0.1:6379&gt; memory stats
 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;peak.allocated&#34;</span>
 2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">3211205544</span>
 3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;total.allocated&#34;</span>
 4<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">875852320</span>
 5<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;startup.allocated&#34;</span>
 6<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">765608</span>
 7<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;replication.backlog&#34;</span>
 8<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">117440512</span>
 9<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;clients.slaves&#34;</span>
10<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">16858</span>
11<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;clients.normal&#34;</span>
12<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">49630</span>
13<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;aof.buffer&#34;</span>
14<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">0</span>
15<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;db.0&#34;</span>
16<span style="color:#ce5c00;font-weight:bold">)</span> 1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;overhead.hashtable.main&#34;</span>
    2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">48388888</span>
    3<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;overhead.hashtable.expires&#34;</span>
    4<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">104</span>
17<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;overhead.total&#34;</span>
18<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">166661600</span>
19<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;keys.count&#34;</span>
20<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">1000007</span>
21<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;keys.bytes-per-key&#34;</span>
22<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">875</span>
23<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;dataset.bytes&#34;</span>
24<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">709190720</span>
25<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;dataset.percentage&#34;</span>
26<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;81.042335510253906&#34;</span>
27<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;peak.percentage&#34;</span>
28<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;27.274873733520508&#34;</span>
29<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
30<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;0.90553224086761475&#34;</span></code></pre></div>
<h3 id="memory-stats细节分析">memory stats细节分析</h3>

<ul>
<li><strong>peak.allocated</strong>: redis从启动来，allocator分配的内存峰值；同于info memory的used_memory_peak</li>
<li><strong>total.allocated</strong>: allocator分配当前内存字节数；同于info memory的used_memory</li>
<li><strong>startup.allocated</strong>: redis启动完成使用的内存字节数；initial_memory_usage; /* Bytes used after initialization. */</li>

<li><p><strong>replication.backlog</strong>: redis复制积压缓冲区(replication backlog)内存使用字节数; 通过repl-backlog-size参数设置，默认1M,上例中redis设置是100MB。（每个实例只有一个backlog）</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#204a87;font-weight:bold">注意</span><span style="color:#a40000">：</span>
<span style="color:#204a87;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">.</span> <span style="color:#204a87;font-weight:bold">redis启用主从同步</span><span style="color:#a40000">，</span><span style="color:#204a87;font-weight:bold">不管backlog是否被填充</span><span style="color:#a40000">，</span><span style="color:#204a87;font-weight:bold">replication</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">backlog都等于repl-backlog-size的值</span><span style="color:#a40000">。</span>
<span style="color:#204a87;font-weight:bold">笔者觉得此值应设置为repl_backlog_histlen更合适</span><span style="color:#a40000">，</span><span style="color:#204a87;font-weight:bold">没太明白大神的用意</span><span style="color:#a40000">。</span>
<span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">.</span> <span style="color:#204a87;font-weight:bold">slave也会启用backlog</span><span style="color:#a40000">；</span><span style="color:#204a87;font-weight:bold">用于slave被提升为master后</span><span style="color:#a40000">，</span>
<span style="color:#204a87;font-weight:bold">仍能使用PSYNC</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">这也是redis4</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span> <span style="color:#204a87;font-weight:bold">PSYNC</span> <span style="color:#204a87;font-weight:bold">2</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0实现的基础</span></code></pre></div></li>

<li><p><strong>clients.slaves</strong>: 在master侧，所有slave clients消耗的内存字节数(非常重要的指标）。
每个slave连接master有且只有一个client, 标识为S<a href="https://link.jianshu.com/?t=https://redis.io/commands/client-list">client list命令中flag为S</a>. 这里消耗的内存指每个slave client的query buffer, client output buffer和client本身结构体占用。
有此指标，就能有效监控和分析slave client消耗的output buffer, 更优化地设置&rdquo;client-output-buffer-limit&rdquo;。
下面示例：当slave client limit设置很大时，可见client的output占用内存非常大，clients.slaves已达3GB. 以前只能通过client list的omem字段分析。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">127.0.0.1:6379&gt; memory stats
1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;peak.allocated&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">38697041192</span>
9<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;clients.slaves&#34;</span>     //因slave client出现大量的client output buffer内存占用
10<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">3312505550</span>
11<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;clients.normal&#34;</span>
12<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">2531130</span></code></pre></div></li>

<li><p><strong>clients.normal</strong>：Redis所有常规客户端消耗内存节字数(非常重要）
即所有flag为N的客户端内存使用: query buffer + client output buffer + client的结构体内存占用。
计算方式和clients.slave类似。 这个子项对于我们监测异常的数据写入或读取的client非常有用。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">memory</span> <span style="color:#000">stats</span>   <span style="color:#8f5902;font-style:italic">//省略其他信息
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#0000cf;font-weight:bold">9</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">clients.slaves</span><span style="color:#4e9a06">&#34;</span>  <span style="color:#8f5902;font-style:italic">// slave client主要占用是client output buffer,见下面id=10520连接
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">integer</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">10256918</span>
<span style="color:#0000cf;font-weight:bold">11</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">clients.normal</span><span style="color:#4e9a06">&#34;</span>   <span style="color:#8f5902;font-style:italic">//普通clients占用的内存，一般是query buffer和client output buffer.
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#0000cf;font-weight:bold">12</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">integer</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">102618310</span>

<span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">client</span> <span style="color:#000">list</span>  <span style="color:#8f5902;font-style:italic">//只显示几个测试clients, 查看omem和qbuf
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10520</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#f57900">xx</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">60592</span> <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">8</span>  <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">S</span>  <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">free</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">2</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10240060</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">rw</span> <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">replconf</span>
<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10591</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#f57900">xx</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">56055</span> <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">N</span>  <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">5799889</span> <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">free</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4440113</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">r</span> <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">set</span>
<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10592</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#f57900">xx</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">56056</span> <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">11</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">N</span>  <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10121401</span> <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">free</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">118601</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">r</span> <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">set</span>
<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10593</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#f57900">xx</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">56057</span> <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">12</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">N</span>  <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">free</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10240002</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">r</span> <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">set</span>
</code></pre></div></li>

<li><p><strong>aof.buffer</strong>： AOF BUFFER使用内存字节数； 一般较小，只有出现AOF rewrite时buffer会变得较大。开启AOF或定期执行BGREWRITEAOF命令的业务，可能使用内存较大，需关注此项目。</p></li>

<li><p><strong>overhead.hashtable.main</strong>：</p></li>

<li><p><strong>overhead.hashtable.expires</strong>：</p></li>

<li><p><strong>overhead.total</strong>：redis额外的总开销内存字节数； 即分配器分配的总内存total.allocated，减去数据实际存储使用内存。
overhead.total由7部分组成，公式如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">计算公式：
overhead.total<span style="color:#ce5c00;font-weight:bold">=</span>startup.allocated + replication.backlog + clients.slaves 
       + clients.normal + aof.buffer + overhead.hashtable.main 
       + overhead.hashtable.expires

实例分析：<span style="color:#ce5c00;font-weight:bold">(</span>见上文 &lt;memory stats的基本使用&gt;节中的实例），通过计算验证正确。
<span style="color:#0000cf;font-weight:bold">765608</span> + <span style="color:#0000cf;font-weight:bold">117440512</span> + <span style="color:#0000cf;font-weight:bold">16858</span> + <span style="color:#0000cf;font-weight:bold">49630</span> + <span style="color:#0000cf;font-weight:bold">0</span> + <span style="color:#0000cf;font-weight:bold">48388888</span> + <span style="color:#000">104</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">166661600</span>
且示例中：17<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;overhead.total&#34;</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">166661600</span></code></pre></div></li>
</ul>

<p><strong>理论应尽量减少额外的内存开销- overhead.total，现在有详细监控，就可以很好入手分析</strong></p>

<ul>
<li><strong>keys.count</strong>: 整个实例key的个数； 相同于dbsize返回值</li>

<li><p><strong>keys.bytes-per-key</strong>：每个key平均占用字节数；把overhead也均摊到每个key上。
不能以此值来表示业务实际的key平均长度。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#a40000">计</span><span style="color:#a40000">算</span><span style="color:#a40000">公</span><span style="color:#a40000">式</span><span style="color:#000;font-weight:bold">:</span>
<span style="color:#000">keys</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">bytes</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">per</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">key</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#a40000">（</span><span style="color:#000">total</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">startup</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">keys</span><span style="color:#000;font-weight:bold">.</span><span style="color:#3465a4">count</span>   
<span style="color:#a40000">实</span><span style="color:#a40000">例</span><span style="color:#a40000">分</span><span style="color:#a40000">析</span><span style="color:#a40000">：</span>
<span style="color:#a40000">（</span><span style="color:#0000cf;font-weight:bold">875852320</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">765608</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#0000cf;font-weight:bold">1000007</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">875.08</span>  <span style="color:#8f5902;font-style:italic">//</span><span style="color:#8f5902;font-style:italic">和</span><span style="color:#8f5902;font-style:italic">上</span><span style="color:#8f5902;font-style:italic">文</span><span style="color:#8f5902;font-style:italic">中</span><span style="color:#8f5902;font-style:italic">的</span><span style="color:#8f5902;font-style:italic">8</span><span style="color:#8f5902;font-style:italic">7</span><span style="color:#8f5902;font-style:italic">5</span><span style="color:#8f5902;font-style:italic">对</span><span style="color:#8f5902;font-style:italic">应</span></code></pre></div></li>

<li><p><strong>dataset.bytes</strong>：表示redis数据占用的内存容量，即分配的内存总量，减去总的额外开销内存量。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">计算公式：
dataset.bytes <span style="color:#ce5c00;font-weight:bold">=</span> total.allocated - overhead.total
实例分析：
<span style="color:#0000cf;font-weight:bold">875852320</span> - <span style="color:#000">166661600</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">709190720</span> // 有上文示例中<span style="color:#4e9a06">&#34;dataset.bytes&#34;</span>值相同</code></pre></div></li>

<li><p><strong>dataset.percentage</strong>：表示redis数据占用内存占总内存分配的百分比（重要）；</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#a40000">计</span><span style="color:#a40000">算</span><span style="color:#a40000">公</span><span style="color:#a40000">式</span><span style="color:#a40000">：</span>
<span style="color:#000">dataset</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">percentage</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dataset</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">bytes</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">total</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">startup</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%</span>
<span style="color:#a40000">实</span><span style="color:#a40000">例</span><span style="color:#a40000">分</span><span style="color:#a40000">析</span><span style="color:#a40000">：</span>
<span style="color:#0000cf;font-weight:bold">709190720</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">875852320</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#0000cf;font-weight:bold">765608</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">81.042336750</span><span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#8f5902;font-style:italic">//有上文示例中的dataset.percentage相同
</span></code></pre></div></li>
</ul>

<blockquote>
<p>可表示业务的redis数据存储的内存效率</p>
</blockquote>

<ul>
<li><p><strong>peak.percentage</strong>：当前内存使用量与峰值时的占比</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#a40000">计</span><span style="color:#a40000">算</span><span style="color:#a40000">公</span><span style="color:#a40000">式</span><span style="color:#a40000">：</span>
<span style="color:#000">peak</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">percentage</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">total</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">peak</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">allocated</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%</span>
<span style="color:#a40000">实</span><span style="color:#a40000">例</span><span style="color:#a40000">分</span><span style="color:#a40000">析</span><span style="color:#a40000">：</span>
<span style="color:#0000cf;font-weight:bold">875852320</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#0000cf;font-weight:bold">3211205544</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">27.274875681393</span><span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#8f5902;font-style:italic">//有上文示例中的peak.percentage相同
</span></code></pre></div></li>

<li><p><strong>fragmentation</strong>: 表示Redis的内存碎片率(非常重要）；前文的项目中都没包含redis内存碎片属性
/* Fragmentation = RSS / allocated-bytes */， 同于info memory中的mem_fragmentation_ratio</p></li>
</ul>

<h3 id="memory-stats的源码实现">memory stats的源码实现</h3>

<h2 id="memory-doctor">memory doctor</h2>

<p>memory doctor命令分析redis使用内存的状态，根据一系列简单判断，给出一定的<strong>诊断建议</strong>,有一定参考价值。</p>

<h3 id="memory-doctor的基本使用">memory doctor的基本使用</h3>

<p>在redis-cli中运行memory doctor命令，如果内存使用有明显不合里的情况，会给出不合理的状态，同时给出处理的建议。
示例如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ruby" data-lang="ruby"><span style="color:#0000cf;font-weight:bold">127</span><span style="color:#ce5c00;font-weight:bold"></span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold"></span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold"></span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold"></span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold"></span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">memory</span> <span style="color:#000">doctor</span>
<span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Sam, I detected a few issues in this Redis instance memory implants:</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">
</span><span style="color:#4e9a06">Peak memory: In the past this instance used more than 150% the memory that is currently using. 
</span><span style="color:#4e9a06">The allocator is normally not able to release memory after a peak, 
</span><span style="color:#4e9a06">so you can expect to see a big fragmentation ratio,
</span><span style="color:#4e9a06">however this is actually harmless and is only due to the memory peak, and if the Redis instance Resident Set Size (RSS) is currently bigger than expected,
</span><span style="color:#4e9a06">the memory will be used as soon as you fill the Redis instance with more data.
</span><span style="color:#4e9a06"> If the memory peak was only occasional and you want to try to reclaim memory, 
</span><span style="color:#4e9a06"> please try the MEMORY PURGE command, otherwise the only other option is to 
</span><span style="color:#4e9a06"> shutdown and restart the instance.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">
</span><span style="color:#4e9a06"> I&#39;m here to keep you safe, Sam. I want to help you.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span></code></pre></div>
<h3 id="memory-doctor细节分析">memory doctor细节分析</h3>

<p>memory doctor主要列举条件判断，满足条件的给出检查结果和建议。
主要包含以下几点，满足其中一点，就给出诊断结果和建议：</p>

<ul>
<li><strong>used_memory小于5M,doctor认为内存使用量过小,不做进一步诊断</strong></li>
<li><strong>peak分配内存大于当前total_allocated的1.5倍，可能说明RSS远大于used_memory</strong></li>
<li><strong>内存碎片率大于1.4</strong></li>
<li><strong>每个Normal Client平均使用内存大于200KB</strong></li>
<li><strong>每个Slave Client平均使用内存大于10MB</strong></li>
</ul>

<h3 id="memory-doctor的源码实现">memory doctor的源码实现</h3>

<p>dockor命令的实现函数getMemoryDoctorReport(void)在object.c源文件
核心代码块如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#000">sds</span> <span style="color:#000">getMemoryDoctorReport</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">void</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">empty</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>          <span style="color:#8f5902;font-style:italic">/* Instance is empty or almost empty. */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">big_peak</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>       <span style="color:#8f5902;font-style:italic">/* Memory peak is much larger than used mem. */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">high_frag</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>      <span style="color:#8f5902;font-style:italic">/* High fragmentation. */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">big_slave_buf</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">/* Slave buffers are too big. */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">big_client_buf</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* Client buffers are too big. */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">num_reports</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">redisMemOverhead</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">mh</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">getMemoryOverheadData</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>  <span style="color:#8f5902;font-style:italic">//获取各个内存指标，用于后面进行条件判断
</span><span style="color:#8f5902;font-style:italic"></span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">mh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">total_allocated</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>  <span style="color:#8f5902;font-style:italic">// 如果使用内存小于5MB,则判断几乎是空实例，不进行其他诊断
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">empty</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">num_reports</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#a40000">省</span><span style="color:#a40000">略</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span>
        <span style="color:#8f5902;font-style:italic">/* Fragmentation is higher than 1.4? */</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">mh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">fragmentation</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#0000cf;font-weight:bold">1.4</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">high_frag</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">num_reports</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>


        <span style="color:#8f5902;font-style:italic">/* Slaves using more than 10 MB each? */</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">numslaves</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">mh</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">clients_slaves</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">numslaves</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">big_slave_buf</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">num_reports</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#000">sds</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">num_reports</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsnew</span><span style="color:#000;font-weight:bold">(</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Hi Sam, I can&#39;t find any memory issue in your instance. </span><span style="color:#4e9a06">&#34;</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">I can only account for what occurs on this base.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">empty</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdsnew</span><span style="color:#000;font-weight:bold">(</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Hi Sam, this instance is empty or is using very little memory, </span><span style="color:#4e9a06">&#34;</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">my issues detector can&#39;t be used in these conditions. </span><span style="color:#4e9a06">&#34;</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Please, leave for your mission on Earth and fill it with some data. </span><span style="color:#4e9a06">&#34;</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">The new Sam and I will be back to our programming as soon as I </span><span style="color:#4e9a06">&#34;</span>
        <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">finished rebooting.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">high_frag</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdscatprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06"> * High fragmentation: This instance has a memory fragmentation greater than 1.4 (this means that the Resident Set Size of the Redis process is much larger than the sum of the logical allocations Redis performed). This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. If the problem is a large peak memory, then there is no issue. Otherwise, make sure you are using the Jemalloc allocator and not the default libc malloc. Note: The currently used allocator is </span><span style="color:#4e9a06">\&#34;</span><span style="color:#4e9a06">%s</span><span style="color:#4e9a06">\&#34;</span><span style="color:#4e9a06">.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">ZMALLOC_LIB</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">big_slave_buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sdscat</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06"> * Big slave buffers: The slave output buffers in this instance are greater than 10MB for each slave (on average). This likely means that there is some slave instance that is struggling receiving data, either because it is too slow or because of networking issues. As a result, data piles on the master output buffers. Please try to identify what slave is not receiving data correctly and why. You can use the INFO output in order to check the slaves delays and the CLIENT LIST command to check the output buffers of each slave.</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">\n</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">big_client_buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>

<span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<h2 id="memory-purge">memory purge</h2>

<p>memory purge命令通过调用jemalloc内部命令，进行内存释放，尽量把redis进程占用但未有效使用内存，即常说的内存碎片释放给操作系统。
memory purge功能只适用于使用jemalloc作为allocator的实例。</p>

<p>redis的*内存碎片率*，是DBA比较头疼的事； 如某个业务下线删除了大量的key,redis不会把“清理”的内存及时归还给操作系统；但这部分内存可以被redis再次利用。</p>

<p>redis4.0提供两种机制解决内存碎片问题，一是memory purge命令； 二是Active memory defragmentation，目前还处于实验阶段，回收效率相当高； 本节只介绍memory purge.</p>

<h3 id="memory-purge的基本使用">memory purge的基本使用</h3>

<p>memory purge使用简单，对性能没明显影响；
通过测试验证来看，内存碎片回收的效率不高
当mem_fragmentation_ratio为2时，执行purge基本没有回收；</p>

<p>下面例子中:内存碎片率mem_fragmentation_ratio为8.2，执行memory purge, 碎片率下降为7.31，回收内存0.28GB。 从4.0版本来看，回收的效率不太理想。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">info</span> <span style="color:#204a87;font-weight:bold">memory</span>
<span style="color:#a40000">#</span> <span style="color:#204a87;font-weight:bold">Memory</span>
<span style="color:#204a87;font-weight:bold">used_memory</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">344944360</span>
<span style="color:#204a87;font-weight:bold">used_memory_human</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">328</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">96M</span>
<span style="color:#204a87;font-weight:bold">used_memory_rss</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">2828042240</span>
<span style="color:#204a87;font-weight:bold">used_memory_rss_human</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">2</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">63G</span>
<span style="color:#204a87;font-weight:bold">mem_fragmentation_ratio</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">8</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">20</span>
<span style="color:#204a87;font-weight:bold">mem_allocator</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">jemalloc-4</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">3</span>
<span style="color:#204a87;font-weight:bold">active_defrag_running</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">0</span>
<span style="color:#204a87;font-weight:bold">lazyfree_pending_objects</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">0</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">memory</span> <span style="color:#204a87;font-weight:bold">purge</span>
<span style="color:#204a87;font-weight:bold">OK</span>
<span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">info</span> <span style="color:#204a87;font-weight:bold">memory</span>
<span style="color:#a40000">#</span> <span style="color:#204a87;font-weight:bold">Memory</span>
<span style="color:#204a87;font-weight:bold">used_memory</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">344942912</span>
<span style="color:#204a87;font-weight:bold">used_memory_human</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">328</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">96M</span>
<span style="color:#204a87;font-weight:bold">used_memory_rss</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">2522521600</span>
<span style="color:#204a87;font-weight:bold">used_memory_rss_human</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">2</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">35G</span>
<span style="color:#204a87;font-weight:bold">used_memory_dataset_perc</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">86</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">02</span><span style="color:#ce5c00;font-weight:bold">%</span>
<span style="color:#204a87;font-weight:bold">mem_fragmentation_ratio</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">7</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">31</span>
<span style="color:#204a87;font-weight:bold">mem_allocator</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">jemalloc-4</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">3</span>
<span style="color:#204a87;font-weight:bold">active_defrag_running</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">0</span>
<span style="color:#204a87;font-weight:bold">lazyfree_pending_objects</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">0</span></code></pre></div>
<h3 id="memory-purge细节分析">memory purge细节分析</h3>

<p>memory purge命令只在jemalloc分配器中有效。
因真正释放内存操作，是通过jemalloc的底层实现，笔者没太看明白；
感兴趣的看官，阅读object.c源文件中的memoryCommand()函数
逻辑代码如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">strcasecmp</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">argv</span><span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">ptr</span><span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">purge</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">c</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">argc</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">if defined(USE_JEMALLOC)  </span><span style="color:#8f5902;font-style:italic">//判断当前redis使用提malloc是否为jemalloc
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">char</span> <span style="color:#000">tmp</span><span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">32</span><span style="color:#000;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#000">narenas</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">size_t</span> <span style="color:#000">sz</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">unsigned</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">je_mallctl</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">arenas.narenas</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">narenas</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">sz</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#8f5902;font-style:italic">//调用jemalloc的处理
</span><span style="color:#8f5902;font-style:italic"></span>            <span style="color:#000">sprintf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">tmp</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">arena.%d.purge</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">narenas</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">je_mallctl</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">tmp</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">NULL</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">addReply</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">shared</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ok</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#204a87;font-weight:bold">return</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">addReplyError</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">Error purging dirty pages</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">else</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">addReply</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">shared</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ok</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">/* Nothing to do for other allocators. */</span>
<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic">endif</span><span style="color:#8f5902;font-style:italic">
</span></code></pre></div>
<h2 id="memory-malloc-stats">memory malloc-stats</h2>

<p>此命令用于打印allocator内部的状态，目前只支持jemalloc。
对于源码开发同学，应该比较有用；
简单示例如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-css" data-lang="css"><span style="color:#204a87;font-weight:bold">127</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">1</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#204a87;font-weight:bold">memory</span> <span style="color:#204a87;font-weight:bold">malloc-stats</span>
<span style="color:#204a87;font-weight:bold">___</span> <span style="color:#204a87;font-weight:bold">Begin</span> <span style="color:#204a87;font-weight:bold">jemalloc</span> <span style="color:#204a87;font-weight:bold">statistics</span> <span style="color:#204a87;font-weight:bold">___</span>
<span style="color:#204a87;font-weight:bold">Version</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">4</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">0</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">3-0-ge9192eacf8935e29fc62fddc2701f7942b1cc02c</span>
<span style="color:#204a87;font-weight:bold">Assertions</span> <span style="color:#204a87;font-weight:bold">disabled</span>
<span style="color:#204a87;font-weight:bold">Run-time</span> <span style="color:#204a87;font-weight:bold">option</span> <span style="color:#204a87;font-weight:bold">settings</span><span style="color:#ce5c00;font-weight:bold">:</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">abort</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">false</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lg_chunk</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">21</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">dss</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;secondary&#34;</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">narenas</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">96</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lg_dirty_mult</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">3</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">arenas</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lg_dirty_mult</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">3</span><span style="color:#ce5c00;font-weight:bold">)</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">stats_print</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">false</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">junk</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;false&#34;</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">quarantine</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">0</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redzone</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">false</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">zero</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">false</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">tcache</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">true</span>
  <span style="color:#204a87;font-weight:bold">opt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lg_tcache_max</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">15</span>
<span style="color:#204a87;font-weight:bold">CPUs</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">24</span>
<span style="color:#204a87;font-weight:bold">Arenas</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">96</span>
<span style="color:#204a87;font-weight:bold">Pointer</span> <span style="color:#204a87;font-weight:bold">size</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">8</span>
<span style="color:#204a87;font-weight:bold">Quantum</span> <span style="color:#204a87;font-weight:bold">size</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">8</span>
<span style="color:#204a87;font-weight:bold">Page</span> <span style="color:#204a87;font-weight:bold">size</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">4096</span>
<span style="color:#204a87;font-weight:bold">Min</span> <span style="color:#204a87;font-weight:bold">active</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">dirty</span> <span style="color:#204a87;font-weight:bold">page</span> <span style="color:#204a87;font-weight:bold">ratio</span> <span style="color:#204a87;font-weight:bold">per</span> <span style="color:#204a87;font-weight:bold">arena</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">8</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">1</span>
<span style="color:#204a87;font-weight:bold">Maximum</span> <span style="color:#204a87;font-weight:bold">thread-cached</span> <span style="color:#204a87;font-weight:bold">size</span> <span style="color:#204a87;font-weight:bold">class</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">32768</span>
<span style="color:#204a87;font-weight:bold">Chunk</span> <span style="color:#204a87;font-weight:bold">size</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">2097152</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">2</span><span style="color:#ce5c00;font-weight:bold">^</span><span style="color:#204a87;font-weight:bold">21</span><span style="color:#ce5c00;font-weight:bold">)</span>
<span style="color:#204a87;font-weight:bold">Allocated</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">345935320</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">active</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">350318592</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">metadata</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">65191296</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">resident</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">455610368</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">mapped</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">2501902336</span>
<span style="color:#204a87;font-weight:bold">Current</span> <span style="color:#204a87;font-weight:bold">active</span> <span style="color:#204a87;font-weight:bold">ceiling</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">352321536</span>

<span style="color:#204a87;font-weight:bold">arenas</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">:</span>
<span style="color:#204a87;font-weight:bold">assigned</span> <span style="color:#204a87;font-weight:bold">threads</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">1</span>
<span style="color:#204a87;font-weight:bold">dss</span> <span style="color:#204a87;font-weight:bold">allocation</span> <span style="color:#204a87;font-weight:bold">precedence</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">secondary</span>
<span style="color:#204a87;font-weight:bold">min</span> <span style="color:#204a87;font-weight:bold">active</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">dirty</span> <span style="color:#204a87;font-weight:bold">page</span> <span style="color:#204a87;font-weight:bold">ratio</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">8</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">1</span>
<span style="color:#204a87;font-weight:bold">dirty</span> <span style="color:#204a87;font-weight:bold">pages</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">85527</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">10020</span> <span style="color:#204a87;font-weight:bold">active</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">dirty</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">82084</span> <span style="color:#204a87;font-weight:bold">sweeps</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">112369</span> <span style="color:#204a87;font-weight:bold">madvises</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">665894</span> <span style="color:#204a87;font-weight:bold">purged</span>
                            <span style="color:#204a87;font-weight:bold">allocated</span>      <span style="color:#204a87;font-weight:bold">nmalloc</span>      <span style="color:#204a87;font-weight:bold">ndalloc</span>    <span style="color:#204a87;font-weight:bold">nrequests</span>
<span style="color:#204a87;font-weight:bold">small</span><span style="color:#ce5c00;font-weight:bold">:</span>                      <span style="color:#204a87;font-weight:bold">311397848</span>     <span style="color:#204a87;font-weight:bold">12825077</span>     <span style="color:#204a87;font-weight:bold">11674091</span>     <span style="color:#204a87;font-weight:bold">32248603</span>
<span style="color:#204a87;font-weight:bold">large</span><span style="color:#ce5c00;font-weight:bold">:</span>                         <span style="color:#204a87;font-weight:bold">983040</span>         <span style="color:#204a87;font-weight:bold">1850</span>         <span style="color:#204a87;font-weight:bold">1842</span>         <span style="color:#204a87;font-weight:bold">1854</span>
<span style="color:#204a87;font-weight:bold">huge</span><span style="color:#ce5c00;font-weight:bold">:</span>                        <span style="color:#204a87;font-weight:bold">33554432</span>            <span style="color:#204a87;font-weight:bold">8</span>            <span style="color:#204a87;font-weight:bold">6</span>            <span style="color:#204a87;font-weight:bold">8</span>
<span style="color:#204a87;font-weight:bold">total</span><span style="color:#ce5c00;font-weight:bold">:</span>                      <span style="color:#204a87;font-weight:bold">345935320</span>     <span style="color:#204a87;font-weight:bold">12826935</span>     <span style="color:#204a87;font-weight:bold">11675939</span>     <span style="color:#204a87;font-weight:bold">32250465</span>
<span style="color:#204a87;font-weight:bold">--省略------</span></code></pre></div>
<h2 id="总结">总结</h2>

<p>memory命令使用我们能直观地查看redis内存分布，对我们掌握内存使用情况，有针对性地做业务的内存使用优化。尤其是purge, stats, usage三个子命令。 相信在新的版本中，memory命令的功能会更加强:)</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis-scan%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE%E9%87%8F%E5%A4%A7%E4%BA%8Elimit%E7%9A%84count%E5%8E%9F%E5%9B%A0%E5%88%86%E6%9E%90/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%B8%89-lazy-free/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>